package com.tnsw.noxnox.util;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.UUID;

import org.apache.http.entity.StringEntity;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;

import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;

import com.tnsw.coreutils.http.THttpBase;
import com.tnsw.coreutils.http.THttpPost;

public class ServerHelper extends THttpPost {
	
	public static int VERSIONCODE = 0;
	public static String VERSIONNAME = "";
	public static String SERVER_URL = "http://172.16.50.192/PredictWeb/GCM.ashx";
	
	private String mTransactionId;
	private String mCommand;
	private ArrayList<JSONParameter> mArgs;
	private String tag;
	private ServerHelperListener listener;
	
	private JSONObject json; 

	public ServerHelper(Context context, String command, ArrayList<JSONParameter> args, ServerHelperListener listener) {
		super(context, SERVER_URL);
		this.tag = "ServerHelper:" + command;
		this.mCommand = command;
		this.listener = listener;
		this.mArgs = args;
		

		this.json = new JSONObject();
				
		try {
			
			this.json.put("command", command);
			this.json.put("versionCode", VERSIONCODE);
			this.json.put("versionName", VERSIONNAME);
			
			JSONObject jArgs = new JSONObject();
			int i;
			if (args != null) {
				for (i = 0; i < args.size(); i++) {
					JSONParameter param = args.get(i);
					jArgs.put(param.key(), param.value());
				}
			}
			
			this.json.put("arguments", jArgs);
		
		} catch (Exception e) {
			Log.e(tag, "Constructor() - " + e.toString(), e);
		}
		
	}
	
	public void run() {
		this.mTransactionId = UUID.randomUUID().toString();
		String requestStr = this.json.toString();
		try {
			this.json.put("tranid", this.mTransactionId);
			requestStr = this.json.toString();
			this.setEntity(new StringEntity(requestStr));
			this.addHeader("Content-Type", "application/json");
			this.addHeader("Host", THttpBase.getHost2(SERVER_URL));
			this.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, "0");
		} catch (Exception e) {
			Log.e(tag, "execute() - " + e.toString(), e);
			
			if (this.listener != null) {
				this.listener.onComplete(this, -2l, "", null, this.mCommand, this.mTransactionId);
			}
		}
		
		
	}
	
	public String transactionId() {
		return(this.mTransactionId);
	}
	
	public String getCommand() {
		return(this.mCommand);
	}
	
	public void setCommand(String command) {
		this.mCommand = command;
	}
	
	public void setArgs(ArrayList<JSONParameter> args) {
		this.mArgs = args;
	}

	@Override
	protected void onPostExecute(Long result) {
		JSONObject jsonObj = null;
		String jsonStr = "";
		String command = "";
		String tranid = "";
		
		super.onPostExecute(result);
		
		if (this.listener != null) {
			
			try {
				
				if (this.getCurrentSB() != null) {
					jsonStr = this.getCurrentSB().toString();
				}
				
				jsonObj = new JSONObject(jsonStr);
				command = jsonObj.getString("command");
				tranid = jsonObj.getString("tranid");
				
				this.listener.onComplete(this, result, jsonStr, jsonObj, command, tranid);
				
			} catch (Exception e) {
				if (result == 0l) {
					result = -3l;
				}
				
				Log.e(tag, "onPostExecute - " + e.toString(), e);
				this.listener.onComplete(this, result, jsonStr, jsonObj, this.mCommand, this.mTransactionId);
			}
			
		}
	}
}
